<%-- 
Copyright [2008] [Manuel Kraft] 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software distributed under
 the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and limitations under the License.

Author     : cutmasta@gmail.com
--%>
<%@ include file="authcheck.jsp" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="org.apache.commons.httpclient.*" %>
<%@ page import="org.apache.commons.httpclient.methods.*" %>
<%@ page import="org.json.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="org.cutmasta.ip.*" %>
<%@ page import="org.apache.commons.httpclient.protocol.Protocol"%>

<html>
    <head>       
        <meta id="viewport" name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"></meta>
        <link rel="stylesheet" type="text/css" href="mystyle.css"/>
        <script type="text/javascript">
            function showDetailHover(node) {
                var body=document.getElementById("body");
                var hover=document.getElementById("hover_details");
                while(hover.firstChild) { hover.removeChild(hover.firstChild); }
            }
        </script>
        <title>Main Content</title>
    </head>
    <body id="body" onload="setTimeout(scrollTo, 0, 0, 1)">
        <div id="hover_details" onClick="this.style.display='none';" style="width:300px;height:300px;background:red;position:absolute;left:10px;top:10px;z-index:100;display:none;">
            
        </div>
        
        <%

          


            User usr = ((User) session.getAttribute("user"));


            Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
            Protocol.registerProtocol("https", easyhttps);

            HttpClient client = usr.getSessionObject().getOxClient();
            
            out.println("<table align=\"left\" border=\"0\" width=\"100%\">");


            out.println("<tr>");
            out.println("<td class=\"oben\" align=\"left\" height=\"30px\">");
            out.println("<a href=\"" + response.encodeURL("content.jsp?module=contacts") + "\"><span class=\"menu\">Kontakte</span></a>");
            out.println("&nbsp;&nbsp;<a href=\"" + response.encodeURL("content.jsp?module=mail") + "\"><span class=\"menu\">Mails</span></a>");
            out.println("&nbsp;&nbsp;<a href=\"" + response.encodeURL("content.jsp?module=calendar") + "\"><span class=\"menu\">Termine</span></a>");
            out.println("&nbsp;&nbsp;<a href=\"" + response.encodeURL("beta.jsp?module=beta") + "\"><span class=\"menu\">BETA</span></a>");
            out.println("&nbsp;&nbsp;<a href=\"" + response.encodeURL("login.jsp?action=logout") + "\"><span class=\"menu\">Logout</span></a>");
            out.println("</td>");
            out.println("</tr/>");




            if (request.getParameter("module") != null && request.getParameter("module").equalsIgnoreCase("contacts")) {



                /*
                1             contact id   0 
                500          displayname 1
                542 	 Telephone business 1 2
                543 	 Telephone business 2 3
                547 	 Telephone company 4
                548 	 Telephone home 1  5	 
                549 	 Telephone home 2 6
                551 	 Cellular telephone 1 7
                552 	 Cellular telephone 2 8
                553 	 Telephone other 9
                555 	 Email 1 10
                558 	 URL 11
                 *                  508          city home 12
                506          street home 13
                 */


                final int CONTACT_FOLDER_ID = usr.getContactFolder(); // currently internal users, should be loaded from config db

                GetMethod contacts_count = new GetMethod("https://" + usr.getOxurl() + "/ajax/contacts?sort=500&order=asc&action=all&order=desc&columns=1,500,542,543,547,548,549,551,552,553,555,558,508,506&folder=" + CONTACT_FOLDER_ID + "&session=" + usr.getSessionObject().getSessionid());

                client.executeMethod(contacts_count);

                JSONObject responseObject = ResponseTools.reponse2JsonObject(contacts_count);
                
                
                contacts_count.releaseConnection();
                
                JSONArray contacts_list = responseObject.getJSONArray("data");
               

                for (int a = 0; a < contacts_list.length(); a++) {
                    out.println("<tr>");


                    JSONArray CONTACT = contacts_list.getJSONArray(a);

                    Integer contact_id = (Integer) CONTACT.get(0);

                    String display_name = (String) CONTACT.get(1);

                    String email = null;
                            
                    if (!CONTACT.isNull(10)) {
                        email = (String) CONTACT.get(10);
                    }                  


                    String cell_1 = null;
                    if (!CONTACT.isNull(7)) {
                        cell_1 = (String) CONTACT.get(7);
                    }

                    String city_home = null;
                    if (!CONTACT.isNull(12)) {
                        city_home = (String) CONTACT.get(12);
                    }

                    String street_home = null;
                    if (!CONTACT.isNull(12)) {
                        street_home = (String) CONTACT.get(13);
                    }



                    //out.println("<td onClick=\"document.getElementById('content_view').src='/oxip/content_view.jsp?action=view_contact&folder=" + CONTACT_FOLDER_ID + "&id=" + contact_id + "'\" class=\"head\">");
                    out.println("<td class=\"head\">");


                    //out.println("<span class=\"dick\">"+display_name+"</span> &nbsp;");

                    out.println("<span class=\"dick\" onClick=\"document.getElementById('hover_details').style.display='block'\">" + display_name + "</span> &nbsp;");



                    if (cell_1 != null) {
                        out.println("<a href=\"tel:" + cell_1 + "\"><span class=\"dick\">" + cell_1 + "</span></a>");
                    }
                    if(email!=null){
                        out.println("<br><a href=\"mailto:" + email + "\"><span class=\"dick\">" + email + "</span><a/><br>");
                    }
                    if (city_home != null && street_home != null) {
                        out.println("<br><a href=\"http://maps.google.com/maps?q=" + street_home + "," + city_home + "\"><span class=\"dick\">Karte</span><a/><br>");
                    } else {
                        if (city_home != null) {
                            out.println("<br><a href=\"http://maps.google.com/maps?q=" + city_home + "\"><span class=\"dick\">Karte</span><a/><br>");
                        }
                    }

                    out.println("</td>");


                    out.println("</tr>");
                }


            } else if (request.getParameter("module") != null && request.getParameter("module").equalsIgnoreCase("calendar")) {
                if (request.getParameter("module") != null && request.getParameter("module").equalsIgnoreCase("delete")) {

                     PutMethod calendar_delete = new PutMethod("https://" + usr.getOxurl() + "/ajax/calendar?action=delete&session=" + usr.getSessionObject().getSessionid());
                     JSONObject js = new JSONObject("data");
                     js.put("id", "1212");
                     calendar_delete.setRequestBody(js.toString());
                     
                     client.executeMethod(calendar_delete);
                     
                     JSONObject responseObject = ResponseTools.reponse2JsonObject(calendar_delete);
                     
                     calendar_delete.releaseConnection();
                     
                     // redirect to appt list
                     response.sendRedirect(response.encodeRedirectURL("/oxip/content.jsp?module=calendar"));
                     
                    
                } else {



                    // show todays appointments                
                    final int CALENDAR_FOLDER_ID = 6; // NOT IN USE ATM, we query all folders

                    Calendar cal = Calendar.getInstance();
                    cal.set(Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    cal.set(Calendar.MILLISECOND, 0);

                    long start = cal.getTimeInMillis();
                    cal.add(Calendar.DAY_OF_MONTH, 7);
                    long end = cal.getTimeInMillis();


                    GetMethod calendar_count = new GetMethod("https://" + usr.getOxurl() + "/ajax/calendar?action=all&sort=202&order=asc&&start=" + start + "&end=" + end + "&columns=1,200,201,202,20&session=" + usr.getSessionObject().getSessionid());

                    client.executeMethod(calendar_count);

                    JSONObject responseObject = ResponseTools.reponse2JsonObject(calendar_count);

                    calendar_count.releaseConnection();

                    JSONArray appt_list = responseObject.getJSONArray("data");

                    SimpleDateFormat sd = new SimpleDateFormat("E, dd.MM yyyy - HH:mm:ss");


                    for (int a = 0; a < appt_list.length(); a++) {

                        /*
                        1, id - 0                  
                        200 - title - 1 ,
                        201 - start date - 2 
                        202 - end date  - 3
                         * 
                         *                  20 - folder id - 4
                         */
                        JSONArray APPOINTMENT = appt_list.getJSONArray(a);


                        Integer appt_id = (Integer) APPOINTMENT.get(0);

                        Integer folder_id = (Integer) APPOINTMENT.get(4);

                        String title = APPOINTMENT.getString(1);

                        Date start_date = new Date(APPOINTMENT.getLong(2));
                        int offset = TimeZone.getTimeZone("Europe/Berlin").getOffset(start_date.getTime());
                        start_date = new Date(start_date.getTime() - offset);

                        String display_start = sd.format(start_date);

                        Date end_date = new Date(APPOINTMENT.getLong(3));
                        int offset_ = TimeZone.getTimeZone("Europe/Berlin").getOffset(end_date.getTime());
                        end_date = new Date(end_date.getTime() - offset_);

                        String display_end = sd.format(end_date);

                        out.println("<tr>");
                        out.println("<td class=\"head\">");
                        out.println("<span class=\"dick\">" + title + "</span><br>" + display_start + "<br>" + display_end + "<br>");
                        //out.println("<a href=\"" + response.encodeRedirectURL("/oxip/content.jsp?module=calendar&action=delete&id=" + appt_id + "&folder_id=" + folder_id + "") + "\"><span class=\"dick\">Entfernen</span></a>");
                        out.println("</td>");
                        out.println("</tr>");
                    }


                }


            } else {

                
                
                
                GetMethod mail_count = new GetMethod("https://" + usr.getOxurl() + "/ajax/mail?action=all&order=desc&columns=600,601,603,607,610&folder=default/INBOX&session=" + usr.getSessionObject().getSessionid());

               

                client.executeMethod(mail_count);
                
                JSONObject responseObject = ResponseTools.reponse2JsonObject(mail_count);                
                
                mail_count.releaseConnection();
                
                
                JSONArray mail_list = responseObject.getJSONArray("data");

                SimpleDateFormat sd = new SimpleDateFormat("E, dd.MM yyyy HH:mm:ss");
                for (int a = 0; a < mail_list.length(); a++) {
                    out.println("<tr>");

                    JSONArray MAIL = mail_list.getJSONArray(a);
                    
                    
                    String mail_id = MAIL.getString(0);
                    String mail_folder = (String) MAIL.get(1);
                    long mail_date = (Long) MAIL.get(4);

                    Date d = new Date(mail_date);


                    String display_date = sd.format(d);

                    out.println("<td onClick=\"document.getElementById('content_view').src='"+ response.encodeURL("content_view.jsp?action=view_mail&folder=" + mail_folder + "&id=" + mail_id)+"'\" class=\"head\">");



                    JSONArray from_data = (JSONArray) (mail_list.getJSONArray(a)).get(2);
                    String subject = (String) (mail_list.getJSONArray(a)).get(3);

                    String sender = from_data.getJSONArray(0).getString(0);
                    if (from_data.getJSONArray(0).isNull(0)) {
                        sender = from_data.getJSONArray(0).getString(1);
                    }

                    out.println("<span class=\"dick\">" + sender + "</span> <span class=\"dick\">" + display_date + "</span> <br>- " + subject + "&nbsp;");
                   

                    out.println("</td>");


                    out.println("</tr>");
                }


            }





            out.println("</table>");
           
            out.println("<iframe id=\"content_view\" height=\"300\" width=\"100%\" style=\"margin:0;border:none\">");
            out.println("</iframe>");

        
        
        %>
        
        
    </body>
    
    
</html>